package com.dongnao.lsn_18_classdemo;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 完成背景
 */
public class MenuBgView extends View {
    public MenuBgView(Context context) {
        this(context,null);
    }

    public MenuBgView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MenuBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    Paint paint;

    Path path;

    public void init(){
        paint = new Paint();
        paint.setAntiAlias(true);
        path = new Path();
    }



    public void setTouchY(float y,float percent){
        path.reset();
        float offset = getHeight() / 8;
        //开始点
        float beginX = 0;
        float beginY = -offset;
        //结束点
        float endX = 0;
        float endY = getHeight() + offset;
        //控制点
        float controlX = getWidth() * percent * 3 / 2;
        float controlY = y;
        path.lineTo(beginX,beginY);
        path.quadTo(controlX,controlY,endX,endY);
        path.close();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawPath(path,paint);
    }



    public void setColor(int color){
        paint.setColor(color);
    }

    public void setColor(Drawable color){
        ColorDrawable colorDrawable = (ColorDrawable) color;
        //背景转换成图片
        paint.setColor(colorDrawable.getColor());

    }


}
